//
//  WQVisitor.swift
//  WeiBo
//
//  Created by Xmen on 16/9/20.
//  Copyright © 2016年 111. All rights reserved.
//

import UIKit

// 访客视图
class WQVisitor: UIView {

    // 点击注册的闭包
    var loginClosure: (() -> ())?

    // 懒加载控件
    // 旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 罩层视图
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 主页视图
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    // 未登录界面
    private lazy var laginLabel: UILabel = {
        let label: UILabel = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        label.numberOfLines = 0

        return label
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        let button: UIButton = UIButton()
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for:.normal)
        // 去掉高亮点击阴影
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        let button: UIButton = UIButton()
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for:.normal)
        // 去掉高亮点击阴影
        button.adjustsImageWhenHighlighted = false
        
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        backgroundColor = UIColor(white: 235 / 255, alpha: 1)
        
        addSubview(cycleImageView)
        addSubview(iconImageView)
        addSubview(maskImageView)
        addSubview(laginLabel)
        addSubview(loginButton)
        addSubview(registerButton)

        
        cycleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        laginLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(maskImageView)
            make.top.equalTo(cycleImageView.snp_bottom)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(laginLabel)
            make.top.equalTo(laginLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(laginLabel.snp_trailing)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }
    }

    func updataVisitorInfo(labeText: String?, imageName: String?) {
        if let  mes = labeText,let imagename = imageName{
        laginLabel.text = mes
        iconImageView.image = UIImage(named: imagename)
        cycleImageView.isHidden = true
        } else {
            startAnimation()
        }
}
    
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false
        
        cycleImageView.layer.add(animation, forKey: nil)
    }
    @objc private func loginAction(){
        print("bibao")
        loginClosure?()
    }
}
